package com.example.reviewdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.reviewdemo.R;
import com.example.reviewdemo.bean.Article;
import com.example.reviewdemo.webview.WebViewActivity;

import java.util.List;

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.articleViewHolder>  {

    private Context context;
    private List<Article> articleList;


    public ArticleAdapter(Context context, List<Article> articleList) {
        this.context = context;
        this.articleList = articleList;
    }

    @NonNull
    @Override
    public articleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.article_item, parent, false);
        return new articleViewHolder(view);
    }

    String link="";

    @Override
    public void onBindViewHolder(@NonNull articleViewHolder holder, int position) {
        Article article = articleList.get(position);
        holder.tv_title.setText(article.getTitle());
        holder.tv_author.setText(article.getAuthor());
        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("link", article.getLink());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return articleList==null?0:articleList.size();
    }




    static class articleViewHolder extends RecyclerView.ViewHolder{

        TextView tv_title,tv_author,tv_link;
        LinearLayout linear;

        ImageView iv_img;

        public articleViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_title=itemView.findViewById(R.id.tv_title);
            tv_author=itemView.findViewById(R.id.tv_author);
            tv_link=itemView.findViewById(R.id.tv_link);
            iv_img=itemView.findViewById(R.id.iv_img);
            linear=itemView.findViewById(R.id.linear);
        }
    }
}
